package com.example.a_java.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.a_java.entity.SysRole;
import com.example.a_java.mapper.SysRoleMapper;
import com.example.a_java.mapper.SysRoleMenuMapper;
import com.example.a_java.service.SysRoleService;
import com.example.a_java.dto.RoleDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createRole(RoleDTO roleDTO) {
        // 转换为实�?
        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleDTO, role);
        
        // 保存角色
        boolean success = save(role);
        
        // 分配菜单权限
        if (success && roleDTO.getMenuIds() != null && !roleDTO.getMenuIds().isEmpty()) {
            assignRoleMenu(role.getRoleId(), roleDTO.getMenuIds());
        }
        
        return success;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRole(RoleDTO roleDTO) {
        // 转换为实�?
        SysRole role = new SysRole();
        BeanUtils.copyProperties(roleDTO, role);
        
        // 更新角色
        boolean success = updateById(role);
        
        // 更新菜单权限
        if (success && roleDTO.getMenuIds() != null) {
            // 先删除原有权�?
            roleMenuMapper.deleteByRoleId(role.getRoleId());
            // 重新分配权限
            if (!roleDTO.getMenuIds().isEmpty()) {
                assignRoleMenu(role.getRoleId(), roleDTO.getMenuIds());
            }
        }
        
        return success;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRole(Long roleId) {
        // 删除角色菜单关联
        roleMenuMapper.deleteByRoleId(roleId);
        // 删除角色
        return removeById(roleId);
    }

    @Override
    public RoleDTO getRoleInfo(Long roleId) {
        // 获取角色信息
        SysRole role = getById(roleId);
        if (role == null) {
            return null;
        }
        
        // 转换为DTO
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(role, roleDTO);
        
        // 获取角色菜单
        List<Long> menuIds = getRoleMenuIds(roleId);
        roleDTO.setMenuIds(menuIds);
        
        return roleDTO;
    }

    @Override
    public List<SysRole> getRolesByUserId(Long userId) {
        return baseMapper.selectRolesByUserId(userId);
    }

    @Override
    public List<String> getRoleKeysByUserId(Long userId) {
        return baseMapper.selectRoleKeysByUserId(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean assignRoleMenu(Long roleId, List<Long> menuIds) {
        // 先删除roleId关联的所有数据
        roleMenuMapper.deleteByRoleId(roleId);
        // 如果menuIds不为空，则插入新的菜单权限数据
        if (menuIds != null && !menuIds.isEmpty()) {
            return roleMenuMapper.batchInsert(roleId, menuIds) > 0;
        }
        // menuIds为空，仅执行删除操作，认为操作成功
        return true;
    }

    @Override
    public List<Long> getRoleMenuIds(Long roleId) {
        return roleMenuMapper.selectMenuIdsByRoleId(roleId);
    }
}
